package bll

import (
	"auth/event"
	"auth/model"
	"auth/model/entity"
	"auth/store"
	"context"
)

type userAuthority struct {
	iUserAuthority store.IUserAuthority
}

var UserAuthority = &userAuthority{}

func (a *userAuthority) init() func() {
	return func() {}
}

func (a *userAuthority) onEvent(*event.Data) {}

func (a *userAuthority) Create(ctx context.Context, in *model.UserAuthorityCreateRequest) error {
	var (
		err error
	)

	c := buildUserAuthority(in)
	_, err = a.iUserAuthority.Create(c)
	return err
}

func (a *userAuthority) List(ctx context.Context, in *model.UserAuthorityListRequest) (*model.UserAuthorityListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.UserAuthority
		out   = &model.UserAuthorityListResponse{}
	)

	if total, list, err = a.iUserAuthority.List(in); err != nil {
		return nil, err
	}

	out.Total = total
	out.Data = model.UserAuthoritysEntityToDto(list)

	return out, err
}

func buildUserAuthority(in *model.UserAuthorityCreateRequest) *entity.UserAuthority {
	return &entity.UserAuthority{

		UserId: in.UserId,

		AuthorityId: in.AuthorityId,
	}
}
